<?php $this->load->view('templates/backend/header'); ?>
<h3><?php echo $this->lang->line('ketoan_export');?></h3>
<style>
    .time{
        width:20%;
    }
    .btn-danger{
        vertical-align: top !important;
    }
</style>
<div class="filter">
    <?php
    echo form_open('/auth/ketoan/');
    echo form_input(array('name' => 'datepicker1', 'id' => 'datepicker1', 'value' => (isset($_POST['datepicker1']))?$_POST['datepicker1']:'','class'=>'time','placeholder'=>'Chọn ngày bắt đầu'));
    echo ' ';

    echo form_button(array('id' => 'export', 'value' => 'export', 'name' => 'submit', 'type' => 'submit', 'content' => $this->lang->line('export').'</a>','class' => 'btn btn-danger'));
    echo form_close();
    ?>
</div>
<span id="simple-content"></span>
<?php
    $this->load->view('templates/backend/footer'); 
    $this->load->view('templates/backend/datepicker'); 
?>

<script>
$("#export").on('click',function(e)
{
    if($("#datepicker1").val() == ""){
        alert("Vui lòng điền ít nhất 1 thông tin cần xử lí");
        return false;
    }
    $("#simple-msg-submit").html("<img src='<?php echo base_url().'static/templates/frontend/home/images/';?>loading.gif'/>");
    setTimeout(function () {
        $.ajax(
            {
                url : '/auth/ketoan/export',
                type: "POST",
                data : { datepicker1: $("#datepicker1").val(), submit: $("#export").val() },
                cache: true,
                beforeSend: function(){

                },
                success:function(data) 
                {
                        if(data.trim()==''){
                            alert('Không tìm thấy thông tin booking nào đặt ngày '+$("#datepicker1").val());return false;
                        }
                        $("#simple-msg-submit").hide();
                        $("#simple-content").html(data);
                        $("#data-table-export").hide();
                        var dt = new Date();
                        var day = dt.getDate();
                        var month = dt.getMonth() + 1;
                        var year = dt.getFullYear();
                        var hour = dt.getHours();
                        var mins = dt.getMinutes();
                        var postfix = day + "." + month + "." + year + "_" + hour + "." + mins;
                        //creating a temporary HTML link element (they support setting file names)
                        var a = document.createElement('a');
                        //getting data from our div that contains the HTML table
                        var data_type = 'data:application/vnd.ms-excel';
                        var table_div = document.getElementById('data-table-export');
                        var table_html = table_div.outerHTML.replace(/ /g, '%20');
//                        console.log(table_html);return false;
                        a.href = data_type + ', ' + table_html;
                        //setting the file name
                        a.download = 'danh_sach_booking_ban_thanh_cong_' + postfix + '.xls';
                        //triggering the function
                        a.click();
                        //just in case, prevent default behaviour
                        e.preventDefault();
                },
                error: function() 
                {
                    $("#simple-msg-submit").html('Có lỗi xảy ra! Contact Admin!');
                }
            });
    }, 1000);
    e.preventDefault();	//STOP default action
});
</script>